﻿#if UNITY_EDITOR 
using System;
using System.IO;
/// <summary>
/// 保存资源抽象
/// </summary>
public abstract class AbsESF_EditorBuildAsset : AbsESF_EditorAsset<AbsESF_EditorBuildAsset>
{
    #region public 构造函数
    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="_fileName">文件名称</param>
    /// <param name="_directory">目录</param>
    /// <param name="_ext">后缀</param>
    public AbsESF_EditorBuildAsset(string _fileName, string _directory,
    enESF_UnityFileExt _ext)
    {
        OnSetFileName(_fileName);
        OnSetDirectory(_directory);
        ext = _ext;
        extAttribute = _ext.ESF_FirstAttribute<ESF_FileExtAttribute>();
        OnReset();
    }
    #endregion

    #region public 属性
    /// <summary>
    /// 文件名称
    /// </summary>
    public string fileName { get; private set; }
    /// <summary>
    /// 文件目录
    /// </summary>
    public string directory { get; private set; }
    /// <summary>
    /// 文件路径
    /// </summary>
    public string filePath { get; private set; }
    /// <summary>
    /// 文件后缀enFileExt
    /// </summary>
    public enESF_UnityFileExt ext { get; private set; }
    /// <summary>
    /// 后缀属性
    /// </summary>
    public ESF_FileExtAttribute extAttribute { get; private set; }
    /// <summary>
    /// 资源是否存在
    /// </summary>
    public bool exists { get { return File.Exists(filePath); } }
    #endregion

    #region OnReset 重置
    /// <summary>
    /// 重置
    /// </summary>
    void OnReset()
    {
        directory = ESF_Utility.TrySubToAssets(directory);
        filePath = Path.Combine(directory, fileName + extAttribute.ext).ESF_TransPathSeparatorCharToUnityChar();
    }
    #endregion

    #region SetFileName 设置文件名称
    /// <summary>
    /// 设置文件名称
    /// </summary>
    /// <param name="_fileName">文件名称</param>
    public void SetFileName(string _fileName)
    {
        OnSetFileName(_fileName);
        OnReset();
    }
    /// <summary>
    /// 设置名称
    /// </summary>
    /// <param name="_fileName">文件名称</param>
    void OnSetFileName(string _fileName)
    {
        fileName = _fileName;
    }
    #endregion

    #region SetDirectory 设置目录
    /// <summary>
    /// 设置目录
    /// </summary>
    /// <param name="_directory">目录</param>
    public void SetDirectory(string _directory)
    {
        OnSetDirectory(_directory);
        OnReset();
    }
    /// <summary>
    /// 设置目录
    /// </summary>
    /// <param name="_directory">目录</param>
    void OnSetDirectory(string _directory)
    {
        directory = _directory;
    }
    #endregion

    #region CreateAsset 创建资源
    /// <summary>
    /// 创建资源
    /// </summary>
    public void CreateAsset()
    {
        OnReset();
        if (!Directory.Exists(directory))
        {
            Directory.CreateDirectory(directory);
        }
        OnCreateAsset();
    }
    /// <summary>
    /// 创建资源
    /// </summary>
    protected abstract void OnCreateAsset();
    #endregion

    #region LoadAsset 加载资源
    /// <summary>
    /// 加载资源
    /// </summary>
    /// <param name="_isAutoCreate">没有资源时自动创建</param>
    public void LoadAsset(bool _isAutoCreate = true)
    {
        if (_isAutoCreate && !exists)
        {
            CreateAsset();
        }
        else
        {
            OnReset();
        }
        OnLoadAsset();
    }
    /// <summary>
    /// 加载资源
    /// </summary>
    protected abstract void OnLoadAsset();
    #endregion

    #region SaveAsset 保存资源
    /// <summary>
    /// 保存资源
    /// </summary>
    /// <returns>true:保存成功,false:保存失败</returns>
    public bool SaveAsset()
    {
        return OnSaveAsset();
    }
    /// <summary>
    /// 保存资源
    /// </summary>
    /// <returns>true:保存成功,false:保存失败</returns>
    protected virtual bool OnSaveAsset() { return true; }
    #endregion

    #region DrawGUI 绘制GUI
    /// <summary>
    /// 绘制GUI
    /// </summary>
    public void DrawGUI() {
        OnDrawGUI();
    }
    /// <summary>
    /// 绘制GUI
    /// </summary>
    protected virtual void OnDrawGUI() { }
    #endregion
}
#endif